package com.ternnetwork.core.notice.controller;

import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.commons.utils.JsonUtils;
import com.ternnetwork.core.notice.dto.NoticeDto;
import com.ternnetwork.core.notice.dto.NoticeQueryDto;
import com.ternnetwork.core.notice.service.NoticeService;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;


@Controller
@RequestMapping("/notice/*")
public class NoticeController {

    private final NoticeService noteService;

    public NoticeController(NoticeService noteService) {
        this.noteService = noteService;
    }

    @PreAuthorize("hasAnyAuthority('API_CORE_NOTICE_ADD')")
    @RequestMapping("/add")
    public @ResponseBody BaseResponse add(String requestJson, Principal user) throws Exception {
        NoticeDto dto= JsonUtils.jsonToObjectThrowsException(requestJson, NoticeDto.class);
        return noteService.add(dto,user.getName());
    }
    @PreAuthorize("hasAnyAuthority('API_CORE_NOTICE_UPDATE')")
    @RequestMapping("/update")
    public @ResponseBody BaseResponse update(String requestJson, Principal user) throws Exception {
        NoticeDto dto= JsonUtils.jsonToObjectThrowsException(requestJson, NoticeDto.class);
        return noteService.update(dto,user.getName());
    }
    @PreAuthorize("hasAnyAuthority('API_CORE_NOTICE_DELETE_BY_ID')")
    @RequestMapping("/deleteById")
    public @ResponseBody BaseResponse deleteById(Long id, Principal user) {
        return noteService.deleteById(id,user.getName());
    }
    @PreAuthorize("hasAnyAuthority('API_CORE_NOTICE_FIND_BY_PAGE')")
    @RequestMapping("/findByPage")
    public @ResponseBody BaseResponse findByPage(@PageableDefault Pageable pageable, NoticeQueryDto dto, Principal user) {
        return noteService.findByPage(pageable, dto,user.getName());
    }

    @PreAuthorize("hasAnyAuthority('API_CORE_NOTICE_FIND_MINE_BY_PAGE')")
    @RequestMapping("/findMineByPage")
    public @ResponseBody BaseResponse findMineByPage(@PageableDefault Pageable pageable, NoticeQueryDto dto, Principal user) {
        dto.setPublished(Boolean.TRUE);
        return noteService.findByPage(pageable, dto,user.getName());
    }

    @RequestMapping("/findMyUnReadByPage")
    public @ResponseBody BaseResponse findMyUnReadByPage(@PageableDefault Pageable pageable, NoticeQueryDto dto, Principal user) {
        dto.setPublished(Boolean.TRUE);
        dto.setRead(Boolean.FALSE);
        return noteService.findByPage(pageable, dto,user.getName());
    }

    @PreAuthorize("hasAnyAuthority('API_CORE_NOTICE_GET_ONE')")
    @RequestMapping("/getOne")
    public @ResponseBody BaseResponse getOne(Long id, Principal user) {
        return noteService.getOne(id,user.getName());
    }

    @PreAuthorize("hasAnyAuthority('API_CORE_NOTICE_PUBLISH_ADD')")
    @RequestMapping("/publish/add")
    public @ResponseBody BaseResponse publish(Long id, Principal user) {
        return noteService.publish(id,user.getName());
    }

    @PreAuthorize("hasAnyAuthority('API_CORE_NOTICE_PUBLISH_DELETE')")
    @RequestMapping("/publish/delete")
    public @ResponseBody BaseResponse deletePublish(Long id, Principal user) {
        return noteService.deletePublish(id,user.getName());
    }

    @RequestMapping("/publish/getOne")
    public @ResponseBody BaseResponse getShareOne(String publishedCode, Principal user) {
        return noteService.getPublishedOne(publishedCode,user.getName());
    }

    @RequestMapping("/countAll")
    public @ResponseBody BaseResponse countAll() {
        return noteService.countAll();
    }



}
